<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="description" content="A fully featured admin theme which can be used to build CRM, CMS, etc.">
    <meta name="author" content="Coderthemes">
	
	<!-- jQuery-->
	<script type="text/javascript" src="/static/plugins/jquery/jquery-3.6.0.min.js"></script>
	
	<!-- layui-->
	<link rel="stylesheet" href="/static/plugins/layui/2.7.6/css/layui.css" media="all">
	<script src="/static/plugins/layui/2.7.6/layui.js" charset="utf-8"></script>
    <title>设置导入字段</title>
</head>
<body>
	<div style="padding: 5px; height: calc(100% - 10px); width: 100%;" data-id="id_item_1">
		<div class="ysui-col flex_none width_percent_100" style="height: 100%;">
			<table class="layui-hide" id="id_main_table" lay-filter="filter_main_table"></table>
		</div>
	</div>
	
	<!-- 序号自增 -->
	<script type="text/html" id="templet_index_no">
	{{d.LAY_TABLE_INDEX+1}}
	</script>
	
	<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="resortImport"><i class="iconfont icon-sorting"></i>调整导入字段顺序</a>
	</div>
	</script>
	
<script type="text/javascript">
var cols = [{
	"title": "序号",
	"fixed": "left",
	"unresize": true,
	"width": "50",
	"align": "center",
	"templet": "#templet_index_no",
	"key": "0-1",
	"hide": false,
	"colspan": 1,
	"rowspan": 1,
	"type": "normal",
	"colGroup": false
},
{
	"title": "字段ID",
	"field": "id",
	"fixed": "left",
	"width": "80",
	"align": "center",
	"key": "0-2",
	"hide": false,
	"colspan": 1,
	"rowspan": 1,
	"type": "normal",
	"colGroup": false
},
{
	"title": "字段名称",
	"field": "name",
	"fixed": "left",
	"width": "120",
	"align": "center",
	"key": "0-3",
	"hide": false,
	"colspan": 1,
	"rowspan": 1,
	"type": "normal",
	"colGroup": false
},
{
	"title": "字段编码",
	"field": "code",
	"fixed": "left",
	"width": "120",
	"align": "center",
	"key": "0-4",
	"hide": false,
	"colspan": 1,
	"rowspan": 1,
	"type": "normal",
	"colGroup": false
},
{
	"title": "是否导入",
	"field": "importState",
	"width": "120",
	"align": "center",
	"key": "0-8",
	"hide": false,
	"colspan": 1,
	"rowspan": 1,
	"type": "normal",
	"colGroup": false,
	templet:function(d){
		return `<input type="checkbox" name="importState" value="` + d.importState + `" lay-skin="switch" lay-text="是|否" lay-filter="_input_filter_checkbox_on_table" ` + (d.importState == 1 ? ` checked ` : ` `) + `>`;
	}
}];

layui.config({
	//自定义layui组件的目录,设定扩展的layui模块的所在目录，一般用于外部模块扩展
	base: '/static/js/layui_extends/'
}).use(['form', 'jquery', 'laydate', 'YsForm'], function(){
	
	// 初始化收件箱
	layui.table.render({
		elem: '#id_main_table'
		,url: '/api/inter/ysCol/search?type=forSettingImport'
		,toolbar: '#toolbarDemo'
		,height: 'full-20' // 最大高度减去其他容器已占有的高度差
		,limit : 50
		,limits : [50,100,200,500]
		,where: {conditions: JSON.stringify([{"rangeType":"t_system","value":"[[${dto.menuId}]]","field":"menuId"}])}
		,cellMinWidth: 95
		,totalRow: true // 开启合计行，true开启，false关闭
		,page: true
		,request: {
			pageName: 'page',
			limitName: 'size'
		}
		,even: false // 开启隔行背景
		,totalRow: true
		,cols: [ cols ]
		,done: function(res){
			
			// 绑定选择框(转换)的值变化提交事件
			layui.form.on('switch(_input_filter_checkbox_on_table)', function(data){
				var value = this.checked ? 1 : 0;
				modifyTableCacheBySwitch(data, value);
			});
		}
		,error: function(res, msg){
			console.log(res, msg)
		}
	});
	// 工具栏事件
	layui.table.on('toolbar(filter_main_table)', function(obj){
		var id = obj.config.id;
		var checkStatus = layui.table.checkStatus(id);
		var datas = checkStatus.data;
		var othis = lay(this);

		switch(obj.event){
			case 'resortImport':
			{
				var w=($(window).width()*0.9);
				var h=($(window).height() - 50);
			    var index = layer.open({
			        type: 2,
			        area: [w+'px', h +'px'],
			        fix: true,
			        maxmin: false,
			        shadeClose: true,
			        shade:0.4,
			        title: "批量导入更新",
			        content: '/web/sys/ysCol/toResortImport?menuId=' + [[${dto.menuId}]]
			    });
				break;
			}
		};
	});
	
	function modifyTableCacheBySwitch(data, value) {
		
	    var table = layui.table;
	    var switchElem = $(data.elem);
	    var tdElem = switchElem.closest('td');
	    var trElem = tdElem.closest('tr');
	    var tableView = trElem.closest('.layui-table-view');
	    table.cache[tableView.attr('lay-id')][trElem.data('index')][tdElem.data('field')] = value;
		
		var dataCommit = {};
		dataCommit['id'] = table.cache[tableView.attr('lay-id')][trElem.data('index')]['id'];
		dataCommit[tdElem.data('field')] = value;
		doEditOnTable(dataCommit);
	}
	
	function doEditOnTable(dataCommit) {
		var t = this;
		var indexOfLoading = layer.load(1, {
			shade: [0.5,'#000000'] //0.1透明度的白色背景
		});
		$.post('/api/inter/ysCol/settingImport', dataCommit, function(res){
			if (res.code == 0) {
				layer.msg(res.msg);
				layer.close(indexOfLoading);
			} else {
				layer.close(indexOfLoading);
				layer.msg(res.msg);
			}
		});
	}
});
</script>
</body>
</html>